package top.pengdev.esoffice.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Pager class
 *
 * @author pengh
 * @description
 * @date 2019-12-25 14:55
 * @JsonInclude null不参与序列化
 **/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Pager {
    private int currentPage;
    private int pageSize = 10;
    private int totalSize;
    private int totalPage;
    private int startLocation;
    private boolean hasFirst;
    private boolean hasPrevious;
    private boolean hasNext;
    private boolean hasLast;

    public Pager() {
    }

    public Pager(int currentPage, int totalSize) {
        this.currentPage = currentPage;
        this.totalSize = totalSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public int getTotalPage() {
        if (pageSize == 0)
            return 0;
        totalPage = totalSize / pageSize;
        if (totalSize % pageSize != 0) {
            totalPage++;
        }
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public boolean isHasFirst() {
        if (currentPage == 1) {
            return false;
        }
        return true;
    }

    public void setHasFirst(boolean hasFirst) {
        this.hasFirst = hasFirst;
    }

    public boolean isHasPrevious() {
        if (isHasFirst()) {
            return true;
        }
        return false;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public boolean isHasNext() {
        if (isHasLast()) {
            return true;
        }
        return false;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public boolean isHasLast() {
        if (currentPage == getTotalPage()) {
            return false;
        }
        return true;
    }

    public void setHasLast(boolean hasLast) {
        this.hasLast = hasLast;
    }

    public int getStartLocation() {
        if (currentPage > 0) {
            return (currentPage - 1) * pageSize;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Pager{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalSize=" + totalSize +
                ", totalPage=" + totalPage +
                ", startLocation=" + startLocation +
                ", hasFirst=" + hasFirst +
                ", hasPrevious=" + hasPrevious +
                ", hasNext=" + hasNext +
                ", hasLast=" + hasLast +
                '}';
    }
}
